package com.cskaoyan.gateway.controller.shopping;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.ICartService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.annotation.Anoymous;
import com.mall.user.intercepter.TokenIntercepter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/shopping")
@Api(tags = "CartsController", description = "购物车控制层")
public class CartController {

    @Reference(timeout = 3000,check = false)
    ICartService cartService;

    @ApiOperation("获得购物车列表")
    @GetMapping("/carts")
    //    @ApiImplicitParam(name = "form", value = "地址信息", dataType = "AddressForm", required = true)
    //    public ResponseData getCartsList(@RequestBody AddressForm form, HttpServletRequest servletRequest) {
    public ResponseData getCartsList(HttpServletRequest request) {
        log.debug("获得购物车列表");

        String userInfo = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());

        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
        cartListByIdRequest.setUserId(uid);
        CartListByIdResponse cartList = cartService.getCartListById(cartListByIdRequest);

        if (!ShoppingRetCode.SUCCESS.getCode().equals(cartList.getCode())) {
            return new ResponseUtil<>().setErrorMsg(cartList.getMsg());
        }
        ResponseData responseData = new ResponseUtil().setData(cartList.getCartProductDtos(), cartList.getMsg());
        return responseData;
    }

    @ApiOperation("添加物品到购物车")
    @PostMapping("/carts")
    public ResponseData addToCart(@RequestBody Map map) {
        log.debug("添加物品到购物车");

        AddCartRequest addCartRequest = new AddCartRequest();
        addCartRequest.setUserId(Long.valueOf(map.get("userId").toString()));
        addCartRequest.setItemId(Long.valueOf(map.get("productId").toString()));
        addCartRequest.setNum(Integer.parseInt(map.get("productNum").toString()));

        AddCartResponse addCartResponse = cartService.addToCart(addCartRequest);
        if (!ShoppingRetCode.SUCCESS.getCode().equals(addCartResponse.getCode())) {
            return new ResponseUtil<>().setErrorMsg(addCartResponse.getMsg());
        }
        return new ResponseUtil().setData(null);
    }

    @ApiOperation("更新购物⻋中的商品")
    @PutMapping("/carts")
    public ResponseData updateCartNum(@RequestBody Map map) {
        log.debug("更新购物⻋中的商品");

        UpdateCartNumRequest updateCartNumRequest = new UpdateCartNumRequest();
        updateCartNumRequest.setUserId(Long.valueOf(map.get("userId").toString()));
        updateCartNumRequest.setItemId(Long.valueOf(map.get("productId").toString()));
        updateCartNumRequest.setNum((Integer) map.get("productNum"));
        updateCartNumRequest.setChecked((String) map.get("checked"));

        UpdateCartNumResponse updateCartNumResponse = cartService.updateCartNum(updateCartNumRequest);
        if (!ShoppingRetCode.SUCCESS.getCode().equals(updateCartNumResponse.getCode())) {
            return new ResponseUtil<>().setErrorMsg(updateCartNumResponse.getMsg());
        }
        return new ResponseUtil().setData(null);
    }

    @ApiOperation("删除购物⻋中的商品")
    @DeleteMapping("/carts/{uid}/{pid}")
    public ResponseData updateCartNum(@PathVariable long uid, @PathVariable long pid) {
        log.debug("删除购物⻋中的商品");

        DeleteCartItemRequest deleteCartItemRequest = new DeleteCartItemRequest();
        deleteCartItemRequest.setUserId(uid);
        deleteCartItemRequest.setItemId(pid);

        DeleteCartItemResponse deleteCartItemResponse = cartService.deleteCartItem(deleteCartItemRequest);
        if (!ShoppingRetCode.SUCCESS.getCode().equals(deleteCartItemResponse.getCode())) {
            return new ResponseUtil<>().setErrorMsg(deleteCartItemResponse.getMsg());
        }
        return new ResponseUtil().setData(deleteCartItemResponse.getMsg());
    }

    @ApiOperation("删除购物车中选中的商品")
    @DeleteMapping("/items/{id}")
    public ResponseData deleteCartItem(@PathVariable long id, HttpServletRequest request) {
//        String userInfo = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
//        JSONObject object = JSON.parseObject(userInfo);
//        Long uid = Long.parseLong(object.get("uid").toString());
        log.debug("删除购物车中选中的商品");

        DeleteCheckedItemRequest deleteCheckedItemRequest = new DeleteCheckedItemRequest();
        deleteCheckedItemRequest.setUserId(id);
        DeleteCheckedItemResposne deleteCheckedItemResposne = cartService.deleteCheckedItem(deleteCheckedItemRequest);

        if (!ShoppingRetCode.SUCCESS.getCode().equals(deleteCheckedItemResposne.getCode())) {
            return new ResponseUtil<>().setErrorMsg(deleteCheckedItemResposne.getMsg());
        }
        return new ResponseUtil().setData(null);

    }




//    @PutMapping("/items")
//    public ResponseData checkAllCartItems(@RequestBody Map map) {
//        CheckAllItemRequest request = new CheckAllItemRequest();
//        request.setChecked((String) map.get("checked"));
//        request.setUserId((Long) map.get("userId"));
//        CheckAllItemResponse response = cartService.checkAllCartItem(request);
//        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
//            return new ResponseUtil().setData(response.getMsg());
//        }
//        return new ResponseUtil().setErrorMsg(response.getMsg());
//
//    }


}
